from myspider.spider import MySpider
import bs4


class XiaoHuaSpider(MySpider):
    def get_start_urls(self):
        return [("http://www.xiaohuar.com/list-1-0.html", self.parse_html)]

    def parse_html(self, response):
        bs = bs4.BeautifulSoup(response.text, features="lxml")
        person_list = bs.find("div", attrs={"class": "demo clearfix"})
        if not person_list:
            raise Exception("Can not find the tag")

        for item in person_list.find_all("div", attrs={"class": "item_list"}):
            print(1111111111111)
            try:
                img_name = item.div.div.div.a.img.attrs["alt"]
                print(img_name)
                img_addr = item.div.div.div.a.img.attrs["src"]
                print(img_addr)
                yield {
                    "img_name": img_name,
                    "img_addr": "http://www.xiaohuar.com" + img_addr,
                }
            except Exception as e:
                print(e)

        pages_url = bs.find("div", attrs={"class": "page"})
        for next_page_url in pages_url.find_all("a", attrs={"class": "page_num"}):
            try:
                yield (next_page_url.a.attrs["href"], self.parse_html)
            except Exception as e:
                pass


xiaohua = XiaoHuaSpider()
xiaohua.crawl()
